package com.example.mystore.mapper;

import com.example.mystore.model.Address;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface AddressMapper {

    @Select("SELECT * FROM address WHERE user_id = #{userId} AND isDel = 0")
    List<Address> findAddressesByUserId(Integer userId);

    @Insert("INSERT INTO `address` (id, user_id, name, phone, mobile, province, city, district, addr, zip, dfault, isDel, created, updated) " +
            "VALUES (#{id}, #{userId}, #{name}, #{phone}, #{mobile}, #{province}, #{city}, #{district}, #{addr}, #{zip}, #{dfault}, #{isDel}, #{created}, #{updated})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insertAddress(Address address);

    @Delete("DELETE FROM `address` WHERE id = #{id}")
    int deleteAddressById(int id);

    @Update("UPDATE `address` SET user_id = #{userId}, name = #{name}, phone = #{phone}, mobile = #{mobile}, province = #{province}, city = #{city}, district = #{district}, " +
            "addr = #{addr}, zip = #{zip}, dfault = #{dfault}, updated = #{updated} WHERE id = #{id}")
    int updateAddress(Address address);

    @Select("SELECT * FROM `address` WHERE id = #{id}")
    Address selectAddressById(int id);

    @Select("SELECT * FROM `address` WHERE user_id = #{id}")
    @Results(id = "addressResultMap", value = {
            @Result(property = "id", column = "id"),
            @Result(property = "userId", column = "user_id"),
            @Result(property = "name", column = "name"),
            @Result(property = "phone", column = "phone"),
            @Result(property = "mobile", column = "mobile"),
            @Result(property = "province", column = "province"),
            @Result(property = "city", column = "city"),
            @Result(property = "district", column = "district"),
            @Result(property = "addr", column = "addr"),
            @Result(property = "zip", column = "zip"),
            @Result(property = "dfault", column = "dfault"),
            @Result(property = "isDel", column = "isDel"),
            @Result(property = "created", column = "created"),
            @Result(property = "updated", column = "updated")
    })
    List<Address> selectAllAddresses(Integer id);

    @Select("SELECT * FROM `address` WHERE id = #{id}")
    @ResultMap("addressResultMap")
    Address getAddressById(@Param("id") Integer id);
}